﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class Bullet : MonoBehaviour {

	private int _owner;
	private int _type;
	public int Type {
		get{ 
			return _type;
		}
		set{
			_type = value;

			Speed = Config.bulletConfig.Speed [_type];
			ATT = Config.bulletConfig.ATT [_type];
			this._owner = _type < 4 ? 0 : (_type < 7 ? 1 : 2);
		}
	}
		
	public float Speed;
	public int ATT;
	public int Direction;
	public void init(int direction){
		this.Direction = direction;
	}



	void OnTriggerEnter(Collider otherObj){

		//print ("子弹触发啦~~~，对方tag = " + otherObj.tag);

		string otherTag = otherObj.gameObject.tag;

		if (otherTag.Equals ("wall") || otherTag.Equals("base_defense")) {
			//print ("子弹碰到墙了。。。，子弹剩余攻击力为：" + _attackLevel);
			if (otherObj.gameObject.GetComponent<Wall> ().SolidLevel <= ATT) {
				if (otherObj.gameObject.transform.parent != null && !otherObj.gameObject.transform.parent.gameObject.name.Equals("BaseDefense")) {
					
					GameObject fa = otherObj.gameObject.transform.parent.gameObject;
				
					//print ("子弹撞到墙了，墙的编号是：" + otherObj.gameObject.name);
					int index = Utils.CalculateInvolve ( Direction, Convert.ToInt32( otherObj.gameObject.name ) );
					//print ("编号为" + otherObj.gameObject.name +  "算出来的index = " + index);
					Transform transform = fa.transform.Find ("" + index);
					if (transform == null) {
						//print ("编号为" + otherObj.gameObject.name + "的 波及的墙已经不存在");
						_gameController.Clear (otherObj.gameObject);
					}
					else {
						//print ("编号为" + otherObj.gameObject.name + "的墙，被击中，准备回收两堵墙");
						_gameController.Clear (otherObj.gameObject);
						_gameController.Clear (transform.gameObject);
					}
				} else {
					_gameController.Clear (otherObj.gameObject);
				}
			}
			Destroy (gameObject);
			//_attackLevel -= wall.SolidLevel;
		} else if (otherTag.Equals ("tank")) {
			Tank tank = otherObj.gameObject.GetComponent<Tank> ();
			if (tank.Type < 4) {  // 敌方
				if (_owner != 0) {
					tank.BeingAttacked (_owner, ATT);
				} else
					return;
			} else if (tank.Type < 7) {
				if (_owner == 0) {
					tank.BeingAttacked (_owner, ATT);
				} else if (_owner == 1) {
					return;
				} else {
					tank.BeingAttacked (_owner, ATT);
				}
			} else {
				if (_owner == 0) {
					tank.BeingAttacked (_owner, ATT);
				} else if (_owner == 1) {
					tank.BeingAttacked (_owner, ATT);
				} else {
					return;
				}
			}
			ATT = 0;
		} else if (otherTag.Equals ("bullet")) {
			if (_owner == otherObj.gameObject.GetComponent<Bullet> ()._owner)
				return;
			Bullet bullet = otherObj.gameObject.GetComponent<Bullet> ();
			if (bullet.ATT == ATT) {
				Destroy (gameObject);
			} else if (bullet.ATT > ATT) {
				bullet.ATT -= ATT;
				Destroy (gameObject);
			}
		} else if (otherTag.Equals ("base")) {
			_gameController.HouseBeAttacked ();
		}

		if(ATT <= 0) Destroy (gameObject);
	}

	private GameController _gameController;
	void Awake() {
		_gameController = GameController.GetInstance ();
	}

	void Update () {
		if (_gameController.Status > 0) {
			float x = 0, y = 0, z = 0;
			if (Direction == 0)
				z = Speed;
			else if (Direction == 1)
				x = Speed;
			else if (Direction == 2)
				z = -Speed;
			else
				x = -Speed;

			transform.Translate (x * Time.deltaTime, y * Time.deltaTime, z * Time.deltaTime);
		}
	}
}
